//
//  ZGuessController.m
//  NewProject
//
//  Created by 陈洪鑫 on 2018/4/13.
//  Copyright © 2018年 chenIos. All rights reserved.
//

#import "ZMyTransactionController.h"
#import "ZTransactionListCell.h"
#import "ZChangeWalletController.h"
#import "ZWalletAccount.h"
#import "ZRecordModel.h"
//#import <Foundation/Foundation.h>
@interface ZMyTransactionController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,assign)NSInteger  page;
@property(nonatomic,strong)UICollectionView *collectionView;
//@property (nonatomic,strong) NSArray *setItems;
@property(nonatomic,strong)ZWalletAccountInfo *account;
@property(nonatomic,strong)NSMutableArray *items;
@end

@implementation ZMyTransactionController
- (NSMutableArray *)items
{
    if (!_items) {
        _items = [NSMutableArray array];
    }return _items;
}
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        CGFloat navHeight = iPhoneX ? 88 + 34 : 64;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H - navHeight) collectionViewLayout:[UICollectionViewFlowLayout new]];
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerNib:[UINib nibWithNibName:@"ZTransactionListCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ZTransactionListCell"];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
    }return _collectionView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = Z_Location(@"transactionLog");
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"change_wallet"] style:(UIBarButtonItemStylePlain) target:self action:@selector(changeWalletAction)];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.collectionView];
     ZWWeakSelf;
    [ZDBManager qureySelectedWallet:^(NSDictionary *dic) {
        weakSelf.account  = [ZWalletAccountInfo mj_objectWithKeyValues:dic];
        weakSelf.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        weakSelf.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
//        weakSelf.title = [weakSelf]
        [weakSelf.collectionView.mj_header beginRefreshing];
    }];
  
}
- (void)loadNewData{
    self.title = [NSString stringWithFormat:@"(%@)%@",self.account.name,Z_Location(@"transactionLog")];
    self.page = 1;
    [self loadData];
}
- (void)loadMoreData{
    [self loadData];
}
- (void)loadData{
   
//    self.page = 1;
    
    
    NSString *url = [NSString stringWithFormat:@"%@wallet/getTansferRecord",PortServer];
    NSDictionary *param = @{
                            @"address":self.account.walletAdress,
                            @"pageIndex":@(self.page),
                            @"pageSize":@10
                            };
    ZWLog(@"url  = %@\nparam = %@",url,param);
    ZWWeakSelf;
    [ZNetManager GET:url parameters:param success:^(NSURLSessionDataTask *task, id  _Nullable responseObject) {
        [weakSelf.collectionView.mj_header endRefreshing];
        [weakSelf.collectionView.mj_footer endRefreshing];
        ZWLog(@"responseObject = %@",responseObject);
        NSInteger status = [responseObject[@"status"] integerValue];
        if (status == 1) {
            NSArray *items = [ZRecordModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]];
            if (weakSelf.page == 1) {
                [weakSelf.items removeAllObjects];
            }
            if (items.count < 10) {
                weakSelf.collectionView.mj_footer.hidden = YES;
                weakSelf.collectionView.backgroundView.hidden = YES;
                
                if (weakSelf.page == 1&&items.count == 0 ) {
                    weakSelf.collectionView.backgroundView = [ZWTools warnNoContentLabelView:@"暂无记录"];
                    weakSelf.collectionView.backgroundView.hidden = NO;
                }
            }else
            {
                weakSelf.collectionView.backgroundView.hidden = YES;
                weakSelf.collectionView.mj_footer.hidden = NO;
            }
            [weakSelf.items addObjectsFromArray:items];
            [weakSelf.collectionView reloadData];
            weakSelf.page ++;
        }else
        {
            [ZProgressManager showError:responseObject[@"message"]];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError *error) {
        [weakSelf.collectionView.mj_header endRefreshing];
        [weakSelf.collectionView.mj_footer endRefreshing];
        [ZProgressManager showError:@"连接失败"];
        ZWLog(@"error = %@",error.userInfo);
    }];
}
- (void)changeWalletAction{
    ZChangeWalletController *changeVC = [ZChangeWalletController new];
    ZWWeakSelf;
    changeVC.ChangeWallet = ^(ZWalletAccountInfo *item) {
        weakSelf.account = item;
        [weakSelf.collectionView.mj_header beginRefreshing];
    };
//    changeVC
    [self.navigationController pushViewController:changeVC animated:YES];
}


#pragma mark -- UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.items.count ? 1 : 0;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.items.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ZTransactionListCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"ZTransactionListCell" forIndexPath:indexPath];
    ZRecordModel *item = self.items[indexPath.row];
    cell.titleLabel.text = item.targetAddress;
    cell.subTitleLabel.text = item.timeSpan;
    cell.imageView.image = [UIImage imageNamed:item.currency];
    if (item.type.integerValue == 1) {
        NSString *str = [NSString stringWithFormat:@"+%@ %@",item.quantity,item.currency];
        NSAttributedString *attr = [[NSAttributedString alloc]initWithString:str attributes:@{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"4296c6"]}];
        cell.transactionLogLabel.attributedText = attr;
    }else if (item.type.integerValue == 2){
        NSString *str = [NSString stringWithFormat:@"-%@ %@",item.quantity,item.currency];
         NSAttributedString *attr = [[NSAttributedString alloc]initWithString:str attributes:@{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"f54848"]}];
        cell.transactionLogLabel.attributedText = attr;
    }
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
    
}


#pragma mark -- UICollectionViewDelegate


#pragma mark -- UICollectionViewDelegateFlowLayout


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = SCREEN_W;
    //    CGFloat height = 180;
    
    return CGSizeMake(width, 82);
    
    
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsZero;
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;



@end
